<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  测试
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  <script>
    ;(function(){
      var startTime = Date.now();
      init();
      
      function init(){
        $.post('/myForm').then(res=>{
          console.log(res);
        }).catch(e=>{
          console.log(e);
        })
      };
      
      window.onbeforeunload = function(e) {
        e = e || window.event;

        var leaveTime = Date.now();

        $.post('/timeOb', {
          startTime,
          leaveTime: leaveTime,
          diffTime: leaveTime - startTime
        },function(res){
          console.log(res)
        })

        // 兼容IE8和Firefox 4之前的版本
        if (e) {
            e.returnValue = "您是否确认离开此页面-您输入的数据可能不会被保存";
        }
        // Chrome, Safari, Firefox 4+, Opera 12+ , IE 9+
        return "您是否确认离开此页面-您输入的数据可能不会被保存";
      };

    })()
    
    
  </script>
</body>
</html>